﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Collections.Generic;

namespace SilverlightMapCreator
{
    public static class Graphics
    {
        public static void SetContrast(double contrast, ref WriteableBitmap wbitmap)
        {

            if (contrast < -100) contrast = -100;
            if (contrast > 100) contrast = 100;

            contrast = (100.0 + contrast) / 100.0;
            contrast *= contrast;


            for (int i = 0; i < wbitmap.Pixels.Length; i++)
            {
                int pixel = wbitmap.Pixels[i];
                Color c = Color.FromArgb(255, (byte)((pixel >> 16) & 0xFF), (byte)((pixel >> 8) & 0xFF), (byte)(pixel & 0xFF));

                double pR = c.R / 255.0;
                pR -= 0.5;
                pR *= contrast;
                pR += 0.5;
                pR *= 255;
                if (pR < 0) pR = 0;
                if (pR > 255) pR = 255;

                double pG = c.G / 255.0;
                pG -= 0.5;
                pG *= contrast;
                pG += 0.5;
                pG *= 255;
                if (pG < 0) pG = 0;
                if (pG > 255) pG = 255;

                double pB = c.B / 255.0;
                pB -= 0.5;
                pB *= contrast;
                pB += 0.5;
                pB *= 255;
                if (pB < 0) pB = 0;
                if (pB > 255) pB = 255;

                int red = (int)pR;
                int green = (int)pG;
                int blue = (int)pB;
                int newColor = (int)255 << 24 | red << 16 | green << 8 | blue;

                wbitmap.Pixels[i] = newColor;
            }
        }

        public static bool Colinear(MyPoint a, MyPoint b, MyPoint c)
        {
            return (b.X - a.X) * (c.Y - a.Y) == (c.X - a.X) * (b.Y - a.Y);
        }

        public static List<MyPoint> RemovePointsOnSameLine(List<MyPoint> route)
        {
            List<MyPoint> output = new List<MyPoint>();

            int count = route.Count - 2;

            for (int i = 0; i < count; i = i + 3)
            {
                MyPoint a = route[i];
                MyPoint b = route[i + 1];
                MyPoint c = route[i + 2];
                if (Graphics.Colinear(a, b, c))
                {
                    output.Add(a);
                    output.Add(c);

                }
                else
                {
                    output.Add(a);
                    output.Add(b);
                    output.Add(c);

                }
            }
            return output;
        }

    }
}
